/*******************************************************************
** This code is part of Breakout.
**
** Breakout is free software: you can redistribute it and/or modify
** it under the terms of the CC BY 4.0 license as published by
** Creative Commons, either version 4 of the License, or (at your
** option) any later version.
******************************************************************/
#include "gameobject.h"


GameObject::GameObject() 
    : mColor(1.0f), mSprite(), mSize(0,0), mIsSolid(false), mDestroyed(false), mPhysicBody(nullptr),mTag(0) 
{ 
}

GameObject::GameObject(b2World *world, glm::vec2 pos, glm::vec2 size, Texture2D sprite, glm::vec3 color)
    : mColor(color), mSprite(sprite), mSize(size), mIsSolid(false), mDestroyed(false), mTag(1)
{
	// static int i=0;
	// i++;
	// printf("with %d,No.%d\n", sprite.ID,i);
	b2BodyDef def;
	def.type = b2_kinematicBody;
	def.position.Set(pos.x, pos.y);
	mPhysicBody = world->CreateBody(&def);
	// set shape
	b2PolygonShape shape;
	shape.SetAsBox(size.x / 2.f, size.y / 2.f);

	// zero-friction and full restitution,and avoid contact of same kind object
	b2FixtureDef boxShapeDef;
	boxShapeDef.shape = &shape;
	boxShapeDef.filter.groupIndex = -1;
	boxShapeDef.friction = 0.0f;
	boxShapeDef.restitution = 1.0f;

	mPhysicBody->CreateFixture(&boxShapeDef);

	mPhysicBody->SetUserData(this);
}

glm::vec2 GameObject::b2vec2toglmvec2(const b2Vec2& vec)
{
	return glm::vec2(vec.x, vec.y);
}

void GameObject::Draw(SpriteRenderer &renderer)
{
    renderer.DrawSprite(this->mSprite, b2vec2toglmvec2(mPhysicBody->GetWorldCenter()), mSize,
									mPhysicBody->GetAngle(), mColor);// radian here not degree
}